package app.kotlin_1.adapter

import android.app.Activity
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import app.kotlin_1.R
import app.kotlin_1.bean.GameBean
import app.kotlin_1.fragment.DataFragment
import app.kotlin_1.utils.e
import app.kotlin_1.utils.inflate
import com.facebook.drawee.view.SimpleDraweeView

/**
 * Created by 刘智强 on 2017/9/25.
 */
class NHomeAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder> {
    companion object {
        private val HOME_BANNER = 0 //首页banner
        private val HOME_CATEGROY = 1 //分类
        private val HOME_CATEGROY_1 = 2
        private val HOME_NOTICE = 3 //公告
        private val HOME_MODEL_DATA = 4 //尾部的数据

    }

    private val viewList = mutableListOf<View>()
    private val newGameList = mutableListOf<GameBean>()
    private val fragmentList = mutableListOf<Fragment>()

    private var context: Activity? = null
    private  var fm :FragmentManager?=null

    constructor(context: Activity?) : super() {
        this.context = context
    }

    constructor(context: Activity?, fm: FragmentManager?) : super() {
        this.context = context
        this.fm = fm
    }


    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        when (holder) {
            is BannerItemViewHolder -> setBannerData(holder)
            is CategroyItemViewHolder -> setCategroyData(holder)
            is CategroyItem1ViewHolder -> setCategroy1Data(holder)
            is NoticeItemViewHolder -> setNoticeData(holder)
            is ModelDataItemViewHolder ->setModelData(holder)
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder =
            when (viewType) {
                HOME_BANNER -> BannerItemViewHolder(parent?.inflate(R.layout.item_banner))
                HOME_CATEGROY -> CategroyItemViewHolder(parent?.inflate(R.layout.item_categroy))
                HOME_CATEGROY_1 -> CategroyItem1ViewHolder(parent?.inflate(R.layout.item_categroy))
                HOME_NOTICE -> NoticeItemViewHolder(parent?.inflate(R.layout.item_notice))
                HOME_MODEL_DATA -> ModelDataItemViewHolder(LayoutInflater.from(parent?.context).inflate(R.layout.item_model_data,parent,false))
                else -> super.createViewHolder(parent, viewType)
            }


    override fun getItemCount(): Int = 5


    override fun getItemViewType(position: Int): Int {
        val i = when (position) {
            0 -> HOME_BANNER
            1 -> HOME_CATEGROY
            2 -> HOME_CATEGROY_1
            3 -> HOME_NOTICE
            4 -> HOME_MODEL_DATA
            else -> {
                -1
            }
        }
        return i
    }

    private fun setBannerData(holder: BannerItemViewHolder) {
        var sv_1 = SimpleDraweeView(context)
        sv_1.setImageURI("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506413113&di=b9839770976ae82cd9eebc9195f51636&imgtype=jpg&er=1&src=http%3A%2F%2Fpic.58pic.com%2F58pic%2F15%2F16%2F30%2F59c58PICQpf_1024.jpg")
        var sv_2 = SimpleDraweeView(context)
        sv_2.setImageURI("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505818393981&di=2b41cd22b674b22829261f683b4eef4f&imgtype=0&src=http%3A%2F%2Fpic5.duowan.com%2Fwow%2F0908%2F114846893719%2F114848423086.jpg")
        var sv_3 = SimpleDraweeView(context)
        sv_3.setImageURI("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505818393981&di=039ed0caaea0f3690c4e6d8853d3f2f6&imgtype=0&src=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201306%2F09%2F103155bqvss11iiv2vsc13.jpg")
        var sv_4 = SimpleDraweeView(context)
        sv_4.setImageURI("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505818393981&di=8446d602f881b1bed0fcff7e8b6998f3&imgtype=0&src=http%3A%2F%2Fimg10.3lian.com%2Fc1%2Fnewpic%2F10%2F24%2F34.jpg")
        viewList.add(sv_1)
        viewList.add(sv_2)
        viewList.add(sv_3)
        viewList.add(sv_4)
        holder.vp_banner?.adapter = object : PagerAdapter() {
            override fun isViewFromObject(view: View?, `object`: Any?): Boolean {
                return view == `object`
            }

            override fun getCount(): Int = viewList.size
            override fun destroyItem(container: ViewGroup?, position: Int, `object`: Any?) {
                container?.removeView(viewList[position])
            }

            override fun instantiateItem(container: ViewGroup?, position: Int): Any {
                container?.addView(viewList[position])
                return viewList[position]
            }

        }


    }

    private fun setCategroyData(holder: CategroyItemViewHolder) {
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505888477527&di=40719d4c0453c8ef489d314cdb2772b0&imgtype=0&src=http%3A%2F%2Fimg.18183.com%2Fuploads%2F170901%2F154-1FZ1155RC18.jpg", "重返部落"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505888550452&di=b009a48dbc9cbe3489b6024411a686cd&imgtype=0&src=http%3A%2F%2Fstatic.ewoka.com%2Fuploads%2Fallimg%2F170824%2F724258_170824180752_1.jpg", "爆蛋军团"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506483384&di=44d34d71207d7907d271354ce7702b2d&imgtype=jpg&er=1&src=http%3A%2F%2Fimage.game.uc.cn%2F2016%2F5%2F26%2F13277166.jpg", "青龙战纪"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506483414&di=435ea05d4cc86c2f7939f3ad595bd87c&imgtype=jpg&er=1&src=http%3A%2F%2Fwww.1688wan.com%2Fallimgs%2Fimage%2F2016%2F05%2F31%2F20160531181747_490.jpg", "自创武林"))
        newGameList.add(GameBean("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3061366639,147781261&fm=27&gp=0.jpg", "曙光破晓"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506483516&di=88ece13a04ce08cfacb3dfee32cf5783&imgtype=jpg&er=1&src=http%3A%2F%2Fimg.9553.com%2Fuploadfile%2F2016%2F0910%2F20160910044818947.jpg", "剑与家园"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505888477527&di=40719d4c0453c8ef489d314cdb2772b0&imgtype=0&src=http%3A%2F%2Fimg.18183.com%2Fuploads%2F170901%2F154-1FZ1155RC18.jpg", "重返部落"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505888550452&di=b009a48dbc9cbe3489b6024411a686cd&imgtype=0&src=http%3A%2F%2Fstatic.ewoka.com%2Fuploads%2Fallimg%2F170824%2F724258_170824180752_1.jpg", "爆蛋军团"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506483384&di=44d34d71207d7907d271354ce7702b2d&imgtype=jpg&er=1&src=http%3A%2F%2Fimage.game.uc.cn%2F2016%2F5%2F26%2F13277166.jpg", "青龙战纪"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506483414&di=435ea05d4cc86c2f7939f3ad595bd87c&imgtype=jpg&er=1&src=http%3A%2F%2Fwww.1688wan.com%2Fallimgs%2Fimage%2F2016%2F05%2F31%2F20160531181747_490.jpg", "自创武林"))
        newGameList.add(GameBean("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3061366639,147781261&fm=27&gp=0.jpg", "曙光破晓"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506483516&di=88ece13a04ce08cfacb3dfee32cf5783&imgtype=jpg&er=1&src=http%3A%2F%2Fimg.9553.com%2Fuploadfile%2F2016%2F0910%2F20160910044818947.jpg", "剑与家园"))
        var adapter = HomeNewGameAdapter(context as Activity, newGameList)
        holder.rv_categroy?.layoutManager = GridLayoutManager(context, 2)
        holder.rv_categroy?.adapter = adapter

    }

    private fun setCategroy1Data(holder: CategroyItem1ViewHolder) {
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505888477527&di=40719d4c0453c8ef489d314cdb2772b0&imgtype=0&src=http%3A%2F%2Fimg.18183.com%2Fuploads%2F170901%2F154-1FZ1155RC18.jpg", "重返部落"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505888550452&di=b009a48dbc9cbe3489b6024411a686cd&imgtype=0&src=http%3A%2F%2Fstatic.ewoka.com%2Fuploads%2Fallimg%2F170824%2F724258_170824180752_1.jpg", "爆蛋军团"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506483384&di=44d34d71207d7907d271354ce7702b2d&imgtype=jpg&er=1&src=http%3A%2F%2Fimage.game.uc.cn%2F2016%2F5%2F26%2F13277166.jpg", "青龙战纪"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506483414&di=435ea05d4cc86c2f7939f3ad595bd87c&imgtype=jpg&er=1&src=http%3A%2F%2Fwww.1688wan.com%2Fallimgs%2Fimage%2F2016%2F05%2F31%2F20160531181747_490.jpg", "自创武林"))
        newGameList.add(GameBean("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3061366639,147781261&fm=27&gp=0.jpg", "曙光破晓"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506483516&di=88ece13a04ce08cfacb3dfee32cf5783&imgtype=jpg&er=1&src=http%3A%2F%2Fimg.9553.com%2Fuploadfile%2F2016%2F0910%2F20160910044818947.jpg", "剑与家园"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505888477527&di=40719d4c0453c8ef489d314cdb2772b0&imgtype=0&src=http%3A%2F%2Fimg.18183.com%2Fuploads%2F170901%2F154-1FZ1155RC18.jpg", "重返部落"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505888550452&di=b009a48dbc9cbe3489b6024411a686cd&imgtype=0&src=http%3A%2F%2Fstatic.ewoka.com%2Fuploads%2Fallimg%2F170824%2F724258_170824180752_1.jpg", "爆蛋军团"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506483384&di=44d34d71207d7907d271354ce7702b2d&imgtype=jpg&er=1&src=http%3A%2F%2Fimage.game.uc.cn%2F2016%2F5%2F26%2F13277166.jpg", "青龙战纪"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506483414&di=435ea05d4cc86c2f7939f3ad595bd87c&imgtype=jpg&er=1&src=http%3A%2F%2Fwww.1688wan.com%2Fallimgs%2Fimage%2F2016%2F05%2F31%2F20160531181747_490.jpg", "自创武林"))
        newGameList.add(GameBean("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3061366639,147781261&fm=27&gp=0.jpg", "曙光破晓"))
        newGameList.add(GameBean("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506483516&di=88ece13a04ce08cfacb3dfee32cf5783&imgtype=jpg&er=1&src=http%3A%2F%2Fimg.9553.com%2Fuploadfile%2F2016%2F0910%2F20160910044818947.jpg", "剑与家园"))
        var adapter = HomeNewGameAdapter(context as Activity, newGameList)
        holder.rv_categroy?.layoutManager = GridLayoutManager(context, 2)
        holder.rv_categroy?.adapter = adapter
    }

    private fun setNoticeData(holder: NoticeItemViewHolder) {
        holder?.tv_message?.text = "这是一个公告这是一个公告这是一个公告"
    }

    private fun setModelData(holder: ModelDataItemViewHolder) {
        var titles = arrayOf("tab1","tab2","tab3","tab4")
        var frament_1 = DataFragment()
        var frament_2 = DataFragment()
        var frament_3 = DataFragment()
        var frament_4 = DataFragment()
        fragmentList.add(frament_1)
        fragmentList.add(frament_2)
        fragmentList.add(frament_3)
        fragmentList.add(frament_4)





        var adapter= object: FragmentPagerAdapter(fm) {
            override fun getItem(position: Int): Fragment {
               return  fragmentList[position]
            }

            override fun getCount(): Int {
                return  titles.size
            }

            override fun getPageTitle(position: Int): CharSequence {
                return titles[position]
            }

        }
        holder.tabLayout?.tabMode = TabLayout.MODE_FIXED
        holder.tabLayout?.newTab()?.let { holder.tabLayout?.addTab(it) }
        holder.tabLayout?.newTab()?.let { holder.tabLayout?.addTab(it) }
        holder.tabLayout?.newTab()?.let { holder.tabLayout?.addTab(it) }
        holder.tabLayout?.newTab()?.let { holder.tabLayout?.addTab(it) }
      if(holder.vpData?.adapter==null)  holder.vpData?.adapter=adapter
        holder.vpData?.currentItem=0
        holder.tabLayout?.setupWithViewPager(holder.vpData)
        holder.tabLayout?.setTabsFromPagerAdapter(adapter)




    }

    inner class BannerItemViewHolder : RecyclerView.ViewHolder {

        var vp_banner: ViewPager? = null;

        constructor(itemView: View?) : super(itemView) {
            vp_banner = itemView?.findViewById(R.id.vp_banner) as ViewPager
        }
    }

    inner class CategroyItemViewHolder : RecyclerView.ViewHolder {

        var rv_categroy: RecyclerView? = null;

        constructor(itemView: View?) : super(itemView) {
            rv_categroy = itemView?.findViewById(R.id.rv_categroy) as RecyclerView
        }
    }

    inner class CategroyItem1ViewHolder : RecyclerView.ViewHolder {
        var rv_categroy: RecyclerView? = null;

        constructor(itemView: View?) : super(itemView) {
            rv_categroy = itemView?.findViewById(R.id.rv_categroy) as RecyclerView
        }
    }

    inner class NoticeItemViewHolder : RecyclerView.ViewHolder {
        var tv_message: TextView? = null

        constructor(itemView: View?) : super(itemView) {
            tv_message = itemView?.findViewById(R.id.tv_message) as TextView
        }
    }

    inner class ModelDataItemViewHolder : RecyclerView.ViewHolder {

        var tabLayout: TabLayout? = null
        var vpData: ViewPager? = null

        constructor(itemView: View?) : super(itemView) {
//            tabLayout = itemView?.findViewById(R.id.tabTitle) as TabLayout
//            vpData = itemView?.findViewById(R.id.vpData) as ViewPager
        }
    }

}